/*
combat_proc.dm
All of the procs used by combat can be found here.
*/

mob
	proc
		calc(stat)
			.=0
			for(var/x in equipment)
				var/obj/gettable/equipment/o = equipment[x]
				if(o && (stat in o.vars))
					. += o.vars[stat]
					. += o.calc_enchant(stat)
			. += vars[stat]

		weight_luck()
			return weighted_rand(
				list(rand(-luck, 0), 0, rand(0, luck)),
				list(10,			50,	30))


		calcOffense()
			return calc("str")+rand(0,calc("skill"))+weight_luck()

		calcDefense()
			return calc("vit")+rand(0,calc("agi"))+weight_luck()

		attacked(mob/m)

		attack(mob/m)                                        //Attack

			var
				offense = calcOffense()
				defense = m.calcDefense()

			if(offense >= defense || prob((luck/3)+1))                //hit check
				var/damage = offense-defense
				if(damage>0)                                 //damage check

					m.attacked(src)
					m.losehealth(damage,src)                     //hurt


				else m.losehealth(0, src)
			else m.losehealth(0, src)

		dropstuff(mob/m)                                     //Drop loot
			for(var/obj/gettable/o in contents)         //things in contents
				if(prob(o.droprate+m.luck))                  //check the drop rate
					o.Move(m.loc)                              //move it to the map
					m << "You found [o]."
					if(isstackable(o))
						var/obj/gettable/stackable/s = o
						s.amount = rand(1,3)

		levelup()                                            //Level up!
			clean_exp()
			if(!client)return
			if(classexp[class]<classmexp[class]) return
			classlvl[class]++
			level++                                         //give level
			maxhp += (round(vit*(classlvl[class]/(sqrt(str)))+10)*gains[class]["hp"])+agi       //health gain
			maxmp += (round(wis*(classlvl[class]/(sqrt(int)*int))+5)*gains[class]["mp"])+skill      //MP gain
			hp = maxhp
			mp = maxmp
			classexp[class]=0                                            //reset exp
			classmexp[class] += round(((classmexp[class]/(level+1))+50 + (150/(level+1)))*gains[class]["curve"])  //calculate new exp
			src << "<font size=3>Level Up ([class]: [classlvl[class]])!</font>"           //messages
			src << "You gained [round(vit*(classlvl[class]/(sqrt(agi)))+10)] HP!"
			src << "You gained [round(skill*(classlvl[class]/(sqrt(str)))+5)] MP!"
			expgive+=(classlvl[class]*3)                          //recalculate exp for killing them

			if(!(classlvl[class]%gains[class]["str"]))
				str++
				src<<"Your strength increased!"
			if(!(classlvl[class]%gains[class]["vit"]))
				vit++
				src<<"Your vitality increased!"
			if(!(classlvl[class]%gains[class]["skill"]))
				skill++
				src<<"Your skill increased!"
			if(!(classlvl[class]%gains[class]["int"]))
				int++
				src<<"Your intelligence increased!"
			if(!(classlvl[class]%gains[class]["wis"]))
				wis++
				src<<"Your wisdom increased!"
			if(!(classlvl[class]%gains[class]["agi"]))
				agi++
				src<<"Your agility increased!"
			if(!(classlvl[class]%gains[class]["luck"]))
				luck++
				src<<"Your luck increased!"

			var/mob/player/p = src
			p.update_stats()

			//if(classskills[class]["[classlvl[class]]"])
			//	skills += classskills[class]["[classlvl[class]]"]

		losehealth(num,k)
			hp-=num
			F_damage(src, num, "#f00", F_Damage.CENTER_ALIGN)

			if(client)
				var/mob/player/p = src
				p.update_hp()

			if(hp<=0 && ismob(k))
				death(k)

		death(mob/killer)
			clean_exp()

			if(client)
				if(!killer.client)
					classexp[class] = max(0, classexp[class]-(classmexp[class]*0.05))
				respawn()

			view() << "[killer] has [isden(src) ? "destroyed" : "killed"] [src]."

			if(!client)
				var/monster/mon = src
				killer.gain_exp(expgive)
				dropstuff(killer)
				if(!mon.den_spawn) make_new_monster(type,initloc,RESPAWN_TIME)
				del src

		respawn()
			hp = round(maxhp/2)
			if(istext(respawn)) respawn = text2path(respawn)
			loc = locate(respawn)
			var/mob/player/p = src
			p.update_bars()

		gain_exp(num)
			if(client)
				clean_exp()
				classexp[class] += num

				F_damage(src, num, "#0f0", F_Damage.LEFT_ALIGN)
				levelup()

				var/mob/player/p = src
				p.update_exp()

		clean_exp()
			if(!client) return
			if(!classexp)classexp=list()
			if(!classmexp)classmexp=list()
			if(!classlvl)classlvl=list()

			if(!(class in classexp))
				classexp += class
				classexp[class]=0
			if(!(class in classmexp))	{classmexp += class; classmexp[class]=100}
			if(!(class in classlvl))	{classlvl += class; classlvl[class]=0}

			if(istext(classexp[class])) classexp[class] = text2num(classexp[class])
			if(istext(classmexp[class])) classmexp[class] = text2num(classmexp[class])
			if(istext(classlvl[class])) classlvl[class] = text2num(classlvl[class])

/*
		coolDown(time,effect)
			coolDowns[effect]=time
			for(isnum(coolDowns[effect]) ? (coolDowns[effect]>0,coolDowns[effect]--) : return)
				sleep(10)

		debuff(time,effect,power)
			spawn(time)
				src<<"Your [effect] buff has run out!"
				bonuses[effect]-=power
*/